﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Facebook;

namespace WebApp.Models
{
    public class Friends
    {
        public class Ref : Friends
        {
            public Ref(String id)
            {
                this.id = id;
            }

            internal void Populate(dynamic flist)
            {
                id = flist["id"];
                name = flist["name"];
            }
        }

        #region Fields

        private String id = null;
        private String name = null;

        #endregion

        #region Properties

        public String Id
        {
            get { return id; }
        }

        public String Name
        {
            get { return name; }
        }

        #endregion 

        private Friends()
        {
        }

        public Friends(String id, String name)
        {
            this.id = id;
            this.name = name;
        }

        /*
         * Method returns the facebook profile's friends
         */

        public static List<Friends> getFriends(String root, String authToken)
        {
            FacebookClient fb = new FacebookClient(authToken);
            dynamic friends = fb.Get(root + "/friends");
            friends = friends["data"];
            List<Friends> fs = new List<Friends>();

            foreach(dynamic friend in friends)
            {
                String f_id = friend["id"];
                Friends.Ref friendRef = new Friends.Ref(f_id);
                // might need something here
                friendRef.Populate(friend);
                fs.Add(friendRef);
            }

            return fs;
        }
    }
}